package com.hoshino.momochi.controller;

import com.hoshino.momochi.model.DTO.HistoryDTO;
import com.hoshino.momochi.model.domain.HistoryRead;
import com.hoshino.momochi.service.HistoryReadService;
import com.hoshino.momochi.util.CodeEnum;
import com.hoshino.momochi.util.JsonResponse;
import com.hoshino.momochi.util.PageCheck;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Slf4j
public class HistoryReadController {

    @Autowired
    private HistoryReadService historyReadService;

    private final String USER_ID = CodeEnum.USERID.getMessage();

    /**
     * 删除 历史记录
     *
     * @param request
     * @param historyDTO 需求字段 history_ids
     * @return
     */
    @RequestMapping("api/history/del")
    public JsonResponse delHistoryByTwoId(HttpServletRequest request, @RequestBody HistoryDTO historyDTO) {
        Long uid = (Long) request.getAttribute(USER_ID);
        List<Long> ids = historyDTO.getHistory_ids();
        if (ids != null) {
            historyReadService.delHistory(uid, ids);
        }
        System.out.println(ids);
        return JsonResponse.success(CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMessage());
    }

    /**
     * 获取用户 历史记录
     *
     * @param request
     * @return
     */
    @RequestMapping("api/history/get")
    public JsonResponse getHistoryByUser(HttpServletRequest request) {
        Long uid = (Long) request.getAttribute(USER_ID);
        HistoryRead historyRead = new HistoryRead();
        historyRead.setUserId(uid);
        return JsonResponse.success(PageCheck.makeData(historyReadService.getHistoryByUserId(historyRead)), CodeEnum.SUCCESS.getMessage());
    }

}
